"""
球队申请模型
"""
from sqlalchemy import Column, String, DateTime, Text, Enum
from datetime import datetime
from app.core.database import Base
import enum


class ApplicationStatus(str, enum.Enum):
    """申请状态"""
    PENDING = "pending"  # 待审批
    APPROVED = "approved"  # 已通过
    REJECTED = "rejected"  # 已拒绝
    CANCELLED = "cancelled"  # 已取消


class TeamApplication(Base):
    """球队申请表"""
    __tablename__ = "team_applications"
    
    id = Column(String(36), primary_key=True)
    team_id = Column(String(36), nullable=False, index=True)  # 球队ID
    team_name = Column(String(200))  # 球队名称（冗余字段，方便查询）
    applicant_id = Column(String(36), nullable=False, index=True)  # 申请人ID
    applicant_name = Column(String(200))  # 申请人昵称
    applicant_avatar = Column(String(500))  # 申请人头像
    status = Column(Enum(ApplicationStatus), default=ApplicationStatus.PENDING, index=True)  # 申请状态
    message = Column(Text)  # 申请留言
    reject_reason = Column(Text)  # 拒绝原因
    reviewed_by = Column(String(36))  # 审批人ID（队长）
    reviewed_at = Column(DateTime)  # 审批时间
    created_at = Column(DateTime, default=datetime.utcnow, index=True)  # 申请时间
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    
    def to_dict(self):
        """转换为字典"""
        return {
            "id": self.id,
            "teamId": self.team_id,
            "teamName": self.team_name,
            "applicantId": self.applicant_id,
            "applicantName": self.applicant_name,
            "applicantAvatar": self.applicant_avatar,
            "status": self.status.value if self.status else None,
            "message": self.message,
            "rejectReason": self.reject_reason,
            "reviewedBy": self.reviewed_by,
            "reviewedAt": self.reviewed_at.isoformat() if self.reviewed_at else None,
            "createdAt": self.created_at.isoformat() if self.created_at else None,
            "updatedAt": self.updated_at.isoformat() if self.updated_at else None
        }

